***************************************************************************************************************
* Name: FigureA3_dataset
* Description: saves dataset used as input for Figure A3
* Reference: "Competition and Civilian Victimization"
***************************************************************************************************************

use "violence_t_year_export.dta",clear
keep if victim_farc~=.&victim_paras~=.

*****Log likelihood definitions*****
set more off

tab action_paras2, gen(prP)
global yP1 "prP1"
global yP2 "prP2"
global yP3 "prP3"

tab action_farc2, gen(prF)
global yF1 "prF1"
global yF2 "prF2"
global yF3 "prF3"

*Types unrestricted

capture program drop lfmsec_ur
program lfmsec_ur
  version 13.1
  *numbers denote actions and letters denote players
  *this model allows heterogeneity across actions but assummes same effect of control variables across players
  args lnf xb2P xb3P xb2F xb3F 
  tempvar p1F p2F p3F p1P p2P p3P 
  quietly {
     
	 gen double `p1F' = 1/(1+exp(`xb2F')+exp(`xb3F'))
     gen double `p2F' = exp(`xb2F')/(1+exp(`xb2F')+exp(`xb3F'))
     gen double `p3F' = exp(`xb3F')/(1+exp(`xb2F')+exp(`xb3F'))
     
	 gen double `p1P' = 1/(1+exp(`xb2P')+exp(`xb3P'))
     gen double `p2P' = exp(`xb2P')/(1+exp(`xb2P')+exp(`xb3P'))
     gen double `p3P' = exp(`xb3P')/(1+exp(`xb2P')+exp(`xb3P'))
     
	 replace `lnf' = $yP1*ln(`p1P') + $yP2*ln(`p2P') + $yP3*ln(`p3P') + $yF1*ln(`p1F') + $yF2*ln(`p2F') + $yF3*ln(`p3F')
   }
end

capture program drop game2stage_types
 program game2stage_types, eclass
	syntax [if]
	marksample touse
	tempname b b1 res_2F_2 res_3F_2 res_2F_3 res_3F_3 res_2P_2 res_3P_2 res_2P_3 res_3P_3
	
		foreach var	 in  nbi_t lpobl_tot_t share_left gini_i army_dist dmr evlp paras_dist farc_dist{
			mkspline `var's = `var', cubic
		}
		
		mkspline royal1 .0000623 royal2 = royalties_t
		mkspline coca1 0.12 coca2 = coca_t

		capture noisily {
		quietly{
			mlogit action_paras2 nbi_ts1-evlps4 paras_dists1-paras_dists4 royal1-coca2 i.year i.year gcaribe gpacifica gorinoquia gamazonia if victim_farc~=., cluster(muni_code) 
			predict predP2,outcome(2)
			predict predP3,outcome(3)
			}
			display "first stage AUC " _rc
		}
		if _rc~=0{
			drop predP2 predP3 nbi_ts1-coca2
			matrix `b'=J(1,200,0)
		}
		else{
			set maxiter 500
			capture noisily {
				quietly{
					mlogit action_farc2 nbi_ts1-evlps4 farc_dists1-farc_dists4 royal1-coca2 i.year gcaribe gpacifica gorinoquia gamazonia if victim_paras~=., cluster(muni_code)
					}
			}  
			display "first stage FARC " _rc		
			if _rc~=0 {
				drop predP2 predP3 nbi_ts1-coca2
				matrix `b'=J(1,200,0)
				}
			else{
				predict predF2,outcome(2)
				predict predF3,outcome(3)
			
				ml model lf lfmsec_ur (eq1: nbi_t royalties_t coca_t share_left lpobl_tot_t army_dist ///
				dmr gini_i year_d1-year_d7 gcaribe gpacifica gorinoquia gamazonia ///
				evlp paras_dist predF2 predF3) /// 
				(eq2: nbi_t royalties_t coca_t share_left lpobl_tot_t army_dist ///
				dmr gini_i year_d1-year_d7 gcaribe gpacifica gorinoquia gamazonia ///
				evlp paras_dist predF2 predF3) /// 
				(eq3: nbi_t royalties_t coca_t share_left lpobl_tot_t army_dist ///
				 dmr gini_i year_d1-year_d7 gcaribe gpacifica gorinoquia gamazonia ///
				evlp farc_dist predP2 predP3) ///
				(eq4: nbi_t royalties_t coca_t share_left lpobl_tot_t army_dist ///
				 dmr gini_i year_d1-year_d7 gcaribe gpacifica gorinoquia gamazonia ///
				evlp farc_dist predP2 predP3),
				capture noisily ml maximize, diff
				display "second stage" _rc
				if _rc~=0{
							matrix `b1'=J(1,96,0)
							matrix `res_2F_2'=J(1,13,0)
							matrix `res_3F_2'=J(1,13,0)
							matrix `res_2F_3'=J(1,13,0)
							matrix `res_3F_3'=J(1,13,0)
							matrix `res_2P_2'=J(1,13,0) 
							matrix `res_3P_2'=J(1,13,0) 
							matrix `res_2P_3'=J(1,13,0) 
							matrix `res_3P_3'=J(1,13,0)
						}
				else{
						matrix `b1'=e(b)
						quietly{
							*Predicted responses of farc 
							margins , at(predP2=(0.2(0.05)0.8) year_d1=0 year_d2=0 year_d3=0 year_d4=0 year_d5=0 year_d6=0 year_d7=0 predP3=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq3)))/(1+exp(predict(equation(eq3)))+exp(predict(equation(eq4))))) nose
							matrix `res_2F_2'=r(b)
							margins , at(predP2=(0.2(0.05)0.8) year_d1=0 year_d2=0 year_d3=0 year_d4=0 year_d5=0 year_d6=0 year_d7=0 predP3=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq4)))/(1+exp(predict(equation(eq3)))+exp(predict(equation(eq4))))) nose
							matrix `res_3F_2'=r(b)

							margins , at(predP3=(0.2(0.05)0.8) year_d1=0 year_d2=0 year_d3=0 year_d4=0 year_d5=0 year_d6=0 year_d7=0 predP2=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq3)))/(1+exp(predict(equation(eq3)))+exp(predict(equation(eq4))))) nose
							matrix `res_2F_3'=r(b)
							margins , at(predP3=(0.2(0.05)0.8) year_d1=0 year_d2=0 year_d3=0 year_d4=0 year_d5=0 year_d6=0 year_d7=0 predP2=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq4)))/(1+exp(predict(equation(eq3)))+exp(predict(equation(eq4))))) nose
							matrix `res_3F_3'=r(b)

							*Predicted responses of paras 
							margins , at(predF2=(0.2(0.05)0.8) year_d1=0 year_d2=0 year_d3=0 year_d4=0 year_d5=0 year_d6=0 year_d7=0 predF3=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq1)))/(1+exp(predict(equation(eq1)))+exp(predict(equation(eq2))))) nose
							matrix `res_2P_2'=r(b)
							margins , at(predF2=(0.2(0.05)0.8) year_d1=0 year_d2=0 year_d3=0 year_d4=0 year_d5=0 year_d6=0 year_d7=0 predF3=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq2)))/(1+exp(predict(equation(eq1)))+exp(predict(equation(eq2))))) nose
							matrix `res_3P_2'=r(b)

							margins , at(predF3=(0.2(0.05)0.8) year_d1=0 year_d2=0 year_d3=0 year_d4=0 year_d5=0 year_d6=0 year_d7=0 predF2=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq1)))/(1+exp(predict(equation(eq1)))+exp(predict(equation(eq2))))) nose
							matrix `res_2P_3'=r(b)
							margins , at(predF3=(0.2(0.05)0.8) year_d1=0 year_d2=0 year_d3=0 year_d4=0 year_d5=0 year_d6=0 year_d7=0 predF2=0.2 gcaribe=0 gpacifica=0 gorinoquia=0 gamazonia=0)  atmeans expression(exp(predict(equation(eq2)))/(1+exp(predict(equation(eq1)))+exp(predict(equation(eq2))))) nose
							matrix `res_3P_3'=r(b)
							}
					}
			matrix `b'=(`b1',`res_2F_2',`res_3F_2',`res_2F_3',`res_3F_3',`res_2P_2',`res_3P_2',`res_2P_3',`res_3P_3')
			drop predF2 predF3 predP2 predP3 nbi_ts1-coca2	
			}


		}	
	ereturn post `b' ,esample(`touse')		
	
end

game2stage_types
matrix b_m=e(b)
matrix res_boot_m=b_m

set seed 1313

set matsize 800

forvalues i = 1/500{
	preserve
	bsample
	game2stage_types
	matrix b_m=e(b)
	matrix res_boot_m=(res_boot_m\b_m)
	display "Iteration: " `i'
	restore
}

svmat double res_boot_m
keep res_boot_m1-res_boot_m200
save "boot_res_region_t_year.dta",replace
